$(function() {
    $("#search_img").click(function() {
        setCurrent(1);
        setFuzzy($("#query_input").val())
        query();
    });
    $("#query_input").keydown(function(e) {
        if (e.which == 13) {
            setCurrent(1);
            setFuzzy($(this).val())
            query();
        }
    });
    $(".order_img").click(function() {
        setCurrent(1);
        if ($("#order").val() == 1) {
            $("#order").val("2");
            $(this).attr("src", "http://oes.com/images/ICN_Increase_10x15.png");
        } else {
            $("#order").val("1");
            $(this).attr("src", "http://oes.com/images/ICN_Decrese_10x15.png");
        }
        query();
    });

    $("#page_size").change(function() {
        setSize($(this).val())
        setCurrent(1);
        query();
    });

    $("#go_btn").click(function() {
        var pageGo = parseInt($("#page_go").val());
        var count = parseInt($("#pageCount").val()); 
        if (0 < pageGo && pageGo <= count) {
            setCurrent(pageGo);
            query();
        } else if (pageGo > count) {
            setCurrent(count);
            query();
        } else {
            setCurrent(1);
            query();
        }
    });
    $("#page_go").keydown(function(e) {
        var pageGo = parseInt($("#page_go").val());
        var count = parseInt($("#pageCount").val()); 
        if (e.which == 13) {
            if (0 < pageGo && pageGo <= count) {
                setCurrent(pageGo);
                query();
            } else if (pageGo > count) {
                setCurrent(count);
                query();
            } else {
                setCurrent(1);
                query();
            }
        }
    });
    $("#page_left").click(function() {
        var current = parseInt($("#currentPage").val());
        if (current == 1) {
            getTip("There is no previous page!");
        }
        if (current > 1) {
            setCurrent(current - 1);
            query();
        }
    });
    $("#page_right").click(function() {
        var current = parseInt($("#currentPage").val());
        var count = parseInt($("#pageCount").val());
        if (current == count) {
            getTip("There is no next page!");
        }
        if (current < count) {
            setCurrent(current + 1);
            query();
        }
    });
})
function setSize(size) {
    $("#pageSize").val(size);
}
function setFuzzy(fuzzy) {
    $("#fuzzyWord").val(fuzzy.trim());
}
function setCurrent(current) {
    $("#currentPage").val(current);
}
function setInfo(size, fuzzy, current, total) {
    $(".query_input").val(fuzzy);
    $("#page_go").val(current);
    $("#page_size").val(size);
    $("#pageCount").val(total);
    $("#total_page").text(current + "/" + total);
}
function getPageBar(current, count) {
    $("#page_index").empty();
    $("#page_index").append(function() {
        var str = "";
        if (count < 7) {
            for (i = 1; i <= count; i++) {
                str += "<span onclick='setIndex(this)' class='group pointer index'>" + i + "</span>";
            }
        } else {
            if(current < 5) {
                for (i = 1; i <= 4; i++) {
                    str += "<span onclick='setIndex(this)' class='group pointer index'>" + i + "</span>"
                }
                str += "<span onclick='setIndex(this)' class='group pointer index'>...</span>";
                str += "<span onclick='setIndex(this)' class='group pointer index'>" + count + "</span>";
            } else if (current > 4 && current < (count - 3)) {
                str += "<span onclick='setIndex(this)' class='group pointer index'>1</span>";
                str += "<span onclick='setDeIndex(this)' class='group pointer index'>...</span>";
                str += "<span onclick='setIndex(this)' class='group pointer index'>" + (current - 1) + "</span>";
                str += "<span onclick='setIndex(this)' class='group pointer index'>" + current + "</span>";
                str += "<span onclick='setIndex(this)' class='group pointer index'>" + (current + 1) + "</span>";
                str += "<span onclick='setIndex(this)' class='group pointer index'>...</span>";
                str += "<span onclick='setIndex(this)' class='group pointer index'>" + count + "</span>";
            } else {
                str += "<span onclick='setIndex(this)' class='group pointer index'>1</span>";
                str += "<span onclick='setDeIndex(this)' class='group pointer index'>...</span>";
                for (i = (count - 3); i <= count; i++) {
                    str += "<span onclick='setIndex(this)' class='group pointer index'>" + i + "</span>";
                }
            }
        }
        return str;
    });
    $(".index").each(function(index, item) {
        if ($(this).text() == current) {
            $(this).addClass("page_active");
        }
    });
}
function setIndex(x) {
    if (x.innerText == "...") {
        setCurrent(eval($("#currentPage").val() + "+ 3"));
    } else {
        setCurrent(x.innerText);
    }
    query();
}
function setDeIndex(x) {
    setCurrent($("#currentPage").val() - 3);
    query();
}
function getTip(content) {
    $(".ms_tip").empty();
    $(".ms_tip").append(content);
    $(".ms_tip").slideDown(1000, function() {
        $(this).slideUp(1000);
    });
}